package com.lindroy.androidplayer.ui.widget

import android.content.Context
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.LayoutInflater
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.isGone
import androidx.core.view.isVisible
import androidx.databinding.DataBindingUtil
import com.lindroy.androidplayer.R
import com.lindroy.androidplayer.databinding.LayoutSettingBarBinding

/**
 * @author Lin
 * @date 2022/2/10
 * @function
 */
class SettingBar @JvmOverloads constructor(
    context: Context,
    private val attr: AttributeSet? = null,
    defaultStyle: Int = 0
) : ConstraintLayout(context, attr, defaultStyle) {

    private var binding: LayoutSettingBarBinding? = null

    private var clickListener: OnItemClickListener? = null

    private var checkedListener: OnCheckedChangeListener? = null

    init {
        //写法一：
        binding = DataBindingUtil.inflate(
            LayoutInflater.from(context),
            R.layout.layout_setting_bar,
            this,
            true
        )
        //写法二：
//        val binding = DataBindingUtil.bind<LayoutSettingBarBinding>(LayoutInflater.from(context).inflate(R.layout.layout_setting_bar, this).also {
//            tag = "layout/layout_setting_bar_0"
//        })
        obtainStyledAttrs()

        binding?.vSettingBarBg?.setOnClickListener { clickListener?.onClick() }

        binding?.switchButton?.setOnCheckedChangeListener { view, isChecked ->
            if (view.isPressed.not()){
                //非人为选中时不监听
                return@setOnCheckedChangeListener
            }
            checkedListener?.onChanged(isChecked)
        }
    }

//    override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean = true

    private fun obtainStyledAttrs() {
        val typeArray = context.obtainStyledAttributes(attr, R.styleable.SettingBar)
        setIcon(typeArray.getDrawable(R.styleable.SettingBar_sb_icon))
        setLabel(typeArray.getString(R.styleable.SettingBar_sb_label).orEmpty())
        setDesc(typeArray.getString(R.styleable.SettingBar_sb_desc))
        showArrow(typeArray.getBoolean(R.styleable.SettingBar_sb_showArrow, true))
        showSwitchButton(typeArray.getBoolean(R.styleable.SettingBar_sb_showSwitch, false))
        setSwitchChecked(typeArray.getBoolean(R.styleable.SettingBar_sb_switchChecked, false))
        typeArray.recycle()
    }

    fun setIcon(drawable: Drawable?) {
        if (drawable == null) {
            return
        }
        binding?.icIcon?.also {
            it.isVisible = true
            it.setImageDrawable(drawable)
        }
    }

    fun setLabel(label: String) {
        binding?.tvLabel?.text = label
    }

    fun setDesc(desc: String?) {
        if (desc.isNullOrBlank()) {
            binding?.tvDesc?.isGone = true
            return
        }
        binding?.tvDesc?.also {
            it.isVisible = true
            it.text = desc
        }
    }

    fun showArrow(isShow: Boolean) {
        binding?.ivArrow?.isVisible = isShow
    }

    fun showSwitchButton(isShow: Boolean) {
        binding?.switchButton?.isVisible = isShow
    }

    fun setSwitchChecked(isChecked: Boolean) {
        binding?.switchButton?.also {
            if (it.isGone) {
                return
            }
            it.isChecked = isChecked
        }
    }

    fun setOnItemClickListener(listener: OnItemClickListener) {
        clickListener = listener
    }

    fun setOnChangedListener(listner: OnCheckedChangeListener) {
        checkedListener = listner
    }

    fun interface OnItemClickListener {
        fun onClick()
    }

    fun interface OnCheckedChangeListener {
        fun onChanged(isChecked: Boolean)
    }

}